#encoding=utf-8
import time,random
from websys import db_redis,webconfig
#key=1_123 玩家编号_游戏_房间号  保存7天
KEY_FORMAT='record_%s_%s_%s'
OBJ_DATA= {'score':0,'allround':0,'winround':0}
rc=db_redis.get_redis()
#obj={score:123,allround:4,winround:1}
#######################################写入###################################################
#玩家坐下
#_id玩家编号，_rid房间号
def UserSitDown(_id,_gid,_rid):
    _key=KEY_FORMAT%(_id,_gid,_rid)
    try:
        rc.hmset(_key,OBJ_DATA)
        rc.expire(_key,60*60*24*webconfig.GAME_RECORD_DAY)
    except:
        print('---UserSitDown--err')

#玩家结算
def UserSettlement(_id,_gid,_rid,_score):
    _key=KEY_FORMAT%(_id,_gid,_rid)
    try:
        if not rc.exists(_key):
            UserSitDown(_id,_gid,_rid)
        _obj=rc.hgetall(_key)
        #print(_obj,'--UserSettlement---',_score)
        _score=int(_score)
        _obj['score']=int(_obj['score'])+_score
        _obj['allround']=int(_obj['allround'])+1
        if(_score)>0:
            _obj['winround']=int(_obj['winround'])+1
        #print(_obj)
        rc.hmset(_key,_obj)
    except:
        print('---UserSettlement--err')

################################读取###########################################
#获取某房间开打的玩家
def GetTable_Palyer(_gid,_rid):
    _rid=str(_rid)
    _key=KEY_FORMAT%('*',_gid,_rid)
    _arr_key=rc.keys(_key)
    _arr_uid=map(lambda x:{'uid':int(x.split('_')[1])},_arr_key)
    return _arr_uid

#获取总局数/赢局数
def GetAllRecrod(_id):
    _all=0
    _win=0
    _key='record_%s_'%(_id)
    _arr_key=rc.keys(_key+'*')
    #_arr_data=rc.#批量
    #按游戏过滤
    res_obj={}
    for ii_key in _arr_key:
        _obj=rc.hgetall(ii_key)
        _gid=ii_key.split('_')[2]
        if(_gid not in res_obj):
            res_obj[_gid]={"id":int(_gid),"round":0,"win":0}
        res_obj[_gid]['round']+=int(_obj['allround'])
        res_obj[_gid]['win']+=int(_obj['winround'])
    return res_obj.values()

#按游戏获取总局数/总分，房间列表
def GetRecordList(_id,_gid=0):
    _gid=int(_gid)
    _all=0
    _win=0
    _key='record_%s_'%(_id)
    if(_gid>0):#某个
        _key+='_'+str(_gid)
    _arr_key=rc.keys(_key+'*')
    #_arr_data=rc.#批量
    res_obj={}
    for ii_key in _arr_key:
        _obj=rc.hgetall(ii_key)
        _rid=ii_key.split('_')[3]
        res_obj[_rid]=_obj
    return res_obj

#获取某个房间的总分，总局
def GetUserTableRecord(_id,_gid,_rid):
    _key=KEY_FORMAT%(_id,_gid,_rid)
    _res={'num':0,'score':0}
    if rc.exists(_key):
        _obj=rc.hgetall(_key)
        _res={'num':int(_obj['allround']),'score':int(_obj['score'])}
    return _res

#获取某个房间的全部人的总分
def GetTableRecordList(_gid,_rid):
    _key=KEY_FORMAT%('*',_gid,_rid)
    _res=[]
    _arr_key=rc.keys(_key)
    for ikey in _arr_key:
        _obj=rc.hgetall(ikey)
        _uid=ikey.split('_')[1]
        _res.append({'uid_id':int(_uid),'score':int(_obj['score'])})
    return _res

#获取玩家的分享

#测试
def testfun():
    for ii in range(1,10):
        UserSettlement(ii,1,123456,random.randint(100,999)-250)
        UserSettlement(ii,1,987654,random.randint(100,999)-150)

#rc.flushdb()
#testfun()
# tkey= rc.keys('record_*')
# for ii in tkey:
#     print(ii,'--->',rc.hgetall(ii))
# for ii in range(1,10):
#     print ii,GetAllRecrod(ii)
#     print ii,GetUserTableRecord(ii,1,123456)
#
# print GetTableRecordList(1,123456)
# print(GetTable_Palyer(1,770870))
# print(GetRecordList(57))
#print(UserSitDown(1,1,123456))
#print(rc.ttl('record_1_1_123456'))